package com.example.nio.niochat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NioClient {
    private int serverPort;
    private String serverIp;
    private SocketChannel socketChannel;

    public NioClient(int serverPort, String serverIp) {
        this.serverPort = serverPort;
        this.serverIp = serverIp;
    }

    public void start() throws IOException {
        socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(true);
        socketChannel.connect(new InetSocketAddress(serverIp, serverPort));
        System.out.println("Connected to " + serverIp + ":" + serverPort);
        ByteBuffer wrap = ByteBuffer.wrap("Hello World".getBytes("UTF-8"));
        socketChannel.write(wrap);

    }

    public static void main(String[] args) throws IOException {
        NioClient client = new NioClient(5000, "localhost");
        client.start();
    }
}
